/*
 * EchoProtocolHandler.java    0.0.1    24/set/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.net.cs.server.impl.mock;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import org.mentalsmash.tazio.net.cs.server.ProtocolHandler;
import org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade;
import org.mentalsmash.tazio.net.cs.server.impl.InputQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @version    ver    24/set/2009    
 * @author     Andrea Reale <andrea.reale@gmail.com>
 */
public class EchoProtocolHandler implements ProtocolHandler {

	private static Logger log = LoggerFactory.getLogger(EchoProtocolHandler.class);
	/* (non-Javadoc)
	 * @see org.mentalsmash.tazio.net.cs.server.impl.ProtocolHandler#handleMessage(java.nio.ByteBuffer, org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade)
	 */
	@Override
	public void handleMessage(ByteBuffer message, ChannelFacade source) {
		log.debug("New complete message received, echoing...");
		source.getOutputQueue().enqueue(message);

	}

	/* (non-Javadoc)
	 * @see org.mentalsmash.tazio.net.cs.server.impl.ProtocolHandler#nextMessage(org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade)
	 */
	@Override
	public ByteBuffer nextMessage(ChannelFacade source) {
		log.debug("Handling new input...");
		
		ByteBuffer toReturn;
		int index=-1;
		InputQueue input = source.getInputQueue();
		byte[] newlineArray  = "\n".getBytes(Charset.forName("UTF-8"));
		
		if(newlineArray.length != 1) {
			throw new RuntimeException("A character should be 1 byte long");
		}
		
		
		index = input.indexOf(newlineArray[0]);
		
		if( index == -1) {
			toReturn = null;
		} else {
			toReturn = input.dequeueBytes(index+1);
		}
		
		return toReturn;
		
	}

	/* (non-Javadoc)
	 * @see org.mentalsmash.tazio.net.cs.server.impl.ProtocolHandler#registered(org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade)
	 */
	@Override
	public void registered(ChannelFacade source) {
		log.debug("Registered");

	}

	/* (non-Javadoc)
	 * @see org.mentalsmash.tazio.net.cs.server.impl.ProtocolHandler#unregistered(org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade)
	 */
	@Override
	public void unregistered(ChannelFacade source) {
		log.debug("UnRegistered");

	}
	
	@Override
	public String toString(){
		return getClass().getSimpleName();
	}

}
